package com.carltechnology.mahjonggamecaculation.dao.impl;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.carltechnology.mahjonggamecaculation.dao.IMahjongDataDao;
import com.carltechnology.mahjonggamecaculation.mode.IBaseData;

import java.util.List;

/**
 * Created by WZTCM on 2016/6/28.
 * The class used for visiting SqliteDataBase.
 * You can call function to get Cursor to get Mode.Or clean database ,update database.
 */
public class MahjongGameDao implements IMahjongDataDao{


    private MahjongGameSqliteHelper helper ;
    private Context mContext;
    private SQLiteDatabase db;
    public MahjongGameDao(Context context) {
        mContext = context;
        helper = new MahjongGameSqliteHelper(mContext, MahjongGameSqliteHelper.DATABASE_INFO.DB_NAME.toString(),null,1);
        db = helper.getWritableDatabase();
    }

    @Override
    public Cursor query(String dbName,String dataOrder) {
        SQLiteDatabase mydatabase = helper.getReadableDatabase();
        return db.query(dbName,null,null,null,null,null,dataOrder);
    }

    @Override
    public Cursor queryById(String dbName, String dataOrder, int id) {
        SQLiteDatabase mydatabase = helper.getReadableDatabase();
        return db.query(dbName,null,"id=?",new String[]{String.valueOf(id)},null,null,dataOrder);
    }

    @Override
    public boolean clearRuntimeTable() {
       // SQLiteDatabase db = helper.getWritableDatabase();
        int count= 0;
        SQLiteDatabase myDb = null;
        try{
            myDb = helper.getWritableDatabase();
            myDb.beginTransaction();
            count=db.delete(String.valueOf(MahjongGameSqliteHelper.DATABASE_INFO.TABLE_RUNTIME_NAME),null,null);

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (myDb!=null){
                myDb.endTransaction();
                myDb.close();
            }

        }
        return count!=0;
    }

    @Override
    public boolean updateTable(String tableName, IBaseData iBaseData, ContentValues values) {
        //SQLiteDatabase db = helper.getWritableDatabase();
        int count = 0;
        synchronized (db){
            count = db.update(tableName,values,"id = ?",new String []{String.valueOf(iBaseData.getId())});
        }
        return count!=0;
    }

    @Override
    public boolean deleteData(String tableName, IBaseData iBaseData) {
        int count = 0;
        synchronized (db){
            count = db.delete(tableName,"id = ?",new String[]{String.valueOf(iBaseData.getId())});
        }
        return count!=0;
    }

    @Override
    public boolean insertData(String tableName, ContentValues values) {
        long count = 0;
        synchronized (db){
            count = db.insert(tableName,null,values);
        }
        return count!=-1;
    }

    @Override
    public boolean insertListDatas(String tableName, List<ContentValues> values) {
        long count = 0;
        for (ContentValues value : values){
            synchronized (db){
                count = db.insert(tableName,null, value);
            }
            if (count==-1) return false;
        }
        return true;
    }

    @Override
    public void closeDataBase() {
        if (db!=null&&db.isOpen()){
            db.close();
        }
    }

    @Override
    public boolean isOpen() {
        if (db==null||!db.isOpen())
            return false;
        return true;
    }

    @Override
    public void openDataBase() {
        if (db!=null&&!db.isOpen()){
            db = helper.getWritableDatabase();
        }
    }

}
